<?php

namespace app\store\model;

use think\Model;

class UserTimeModel extends Model
{
    protected $connection = 'db_platform_accounts';

    protected $table = 'game_user_time';


    /**
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getTimeDistribution($where)
    {
        $arr = [
            [
                0, 0, 0, 0, 0, 0, 0, 0, 0
            ],
            [
                0, 0, 0, 0, 0, 0, 0, 0, 0
            ]
        ];
        $data = self::where($where)
            ->fieldRaw('sum(GameTime) as my_time,type')
            ->group('UserID,time')
            ->select();
        $total = 0;
        foreach ($data as $v) {
            if($v['type'] == 0){
                $total+=$v['my_time'];
            }
            if ($v['my_time'] <= 10) {
                $arr[$v['type']][0]++;
            } else if ($v['my_time'] >= 11 and $v['my_time'] <= 60) {
                $arr[$v['type']][1]++;
            } else if ($v['my_time'] > 60 and $v['my_time'] <= 3 * 60) {
                $arr[$v['type']][2]++;
            } else if ($v['my_time'] > 3 * 60 and $v['my_time'] <= 10 * 60) {
                $arr[$v['type']][3]++;
            } else if ($v['my_time'] > 10 * 60 and $v['my_time'] <= 30 * 60) {
                $arr[$v['type']][4]++;
            } else if ($v['my_time'] > 30 * 60 and $v['my_time'] <= 60 * 60) {
                $arr[$v['type']][5]++;
            } else if ($v['my_time'] > 60 * 60 and $v['my_time'] <= 2 * 60 * 60) {
                $arr[$v['type']][6]++;
            } else if ($v['my_time'] > 2 * 60 * 60 and $v['my_time'] <= 4 * 60 * 60) {
                $arr[$v['type']][7]++;
            } else if ($v['my_time'] > 4 * 60 * 60) {
                $arr[$v['type']][8]++;
            }
        }

        // 活跃和新增分别求和
        $arr[] = array_sum($arr[0]);
        $arr[] = array_sum($arr[1]);
        $arr['total_time'] = $total;

        return $arr;
    }
}